﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NHibernate.Linq;
using NhJump.Persistence.Context;
using NhJump.Persistence.Model;
using NhJump.Persistence.Querying.Executable;

namespace NhJump.Persistence.Querying.Definition
{
    public class LinqQueryDefinition<T, TIdentifier> : IQueryDefinition<T, TIdentifier>
        where T : IDomainEntity<TIdentifier>
    {
        private readonly Expression<Func<T, bool>> _expression;

        public LinqQueryDefinition(Expression<Func<T, bool>> expression)
        {
            _expression = expression;
        }

        public IExecutableQuery<T, TIdentifier> ToExecutableQuery(IReadableRepository<T, TIdentifier> readableRepository, IUnitOfWork unitOfWork)
        {
            return new LinqQuery<T, TIdentifier>(unitOfWork.Session.Linq<T>().Where(_expression), unitOfWork);
        }
    }
}
